﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class CustomerContactorInfo : BasePage
    {
        private SystemBll sysBll = new SystemBll();

        public Guid CustomerID
        {
            get
            {
                if (hiddenCustomerID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenCustomerID.Value);
            }
            set
            {
                hiddenCustomerID.Value = value.ToString();
            }
        }
        public Guid ContactorID
        {
            get
            {
                if (hiddenContactorID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenContactorID.Value);
            }
            set
            {
                hiddenContactorID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initForm();
            }
        }

        private void initForm()
        {
            if (Request.QueryString["CustomerID"] != null && Request.QueryString["CustomerID"] != "")
            {
                CustomerID = new Guid(Request.QueryString["CustomerID"].ToString());
            }
            if (Request.QueryString["ContactorID"] != null && Request.QueryString["ContactorID"] != "") {
                ContactorID = new Guid(Request.QueryString["ContactorID"].ToString());
            }
            if (ContactorID != Guid.Empty) {
                WsmCustomerContactorEntity entity = new SystemBll().Get<WsmCustomerContactorEntity>(ContactorID);
                ContactorDuty.Text = entity.ContactorDuty;
                ContactorEmal.Text = entity.ContactorEmal;
                ContactorName.Text = entity.ContactorName;
                ContactorPhone.Text = entity.ContactorPhone;
                CustomerID = entity.CustomerID;
            }
        }

        private void save()
        {
            WsmCustomerContactorEntity entity = new WsmCustomerContactorEntity() ;
            if (ContactorID != Guid.Empty)
            {
                entity = new SystemBll().Get<WsmCustomerContactorEntity>(ContactorID);
            } else{
                entity.CustomerID = CustomerID;
                entity.Deleted = false;
                entity.ContactorID = Guid.NewGuid();
            }                        
            entity.ContactorDuty = ContactorDuty.Text.Trim();
            entity.ContactorEmal = ContactorEmal.Text.Trim();
            entity.ContactorName = ContactorName.Text.Trim();
            entity.ContactorPhone = ContactorPhone.Text.Trim();         
            sysBll.InsertOrUpdate(entity);                         
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (!checkData()) { return; }
            save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (!checkData()) { return; }
            save();
            ContactorDuty.Text ="";
            ContactorEmal.Text = "";
            ContactorName.Text = "";
            ContactorPhone.Text = "";
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');", true);
        }
        private Boolean checkData()
        {
            if (ContactorName.Text.IndexOf("老师") > 0)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "alert('请输入正确的联系人姓名');", true);
                return false;
            }

            if (ContactorEmal.Text == "" && ContactorPhone.Text == "")
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "alert('请输入联系方式（电话或邮件地址）');", true);
                return false;
            }
            return true;

        }
    }
}
